/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本软件已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2019-2021 十三 all rights reserved.
 * 版权所有，侵权必究！
 */
package ltd.nexgen.mall.api.mall;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import ltd.nexgen.mall.api.mall.param.SaveMallUserAddressParam;
import ltd.nexgen.mall.api.mall.param.UpdateMallUserAddressParam;
import ltd.nexgen.mall.api.mall.vo.NewBeeMallUserAddressVO;
import ltd.nexgen.mall.common.ServiceResultEnum;
import ltd.nexgen.mall.config.annotation.TokenToMallUser;
import ltd.nexgen.mall.entity.MallUser;
import ltd.nexgen.mall.entity.MallUserAddress;
import ltd.nexgen.mall.service.NewBeeMallUserAddressService;
import ltd.nexgen.mall.util.BeanUtil;
import ltd.nexgen.mall.util.Result;
import ltd.nexgen.mall.util.ResultGenerator;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@RestController
@Tag(name = "6.新蜂商城个人地址相关接口", description = "v1")
@RequestMapping("/api/v1")
public class NewBeeMallUserAddressAPI {

    @Resource
    private NewBeeMallUserAddressService mallUserAddressService;

    @GetMapping("/address")
    @Operation(summary = "我的收货地址列表")
    public Result<List<NewBeeMallUserAddressVO>> addressList(
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        return ResultGenerator.genSuccessResult(mallUserAddressService.getMyAddresses(loginMallUser.getUserId()));
    }

    @PostMapping("/address")
    @Operation(summary = "添加地址")
    public Result<Boolean> saveUserAddress(
            @RequestBody SaveMallUserAddressParam saveMallUserAddressParam,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        MallUserAddress userAddress = new MallUserAddress();
        BeanUtil.copyProperties(saveMallUserAddressParam, userAddress);
        userAddress.setUserId(loginMallUser.getUserId());
        Boolean saveResult = mallUserAddressService.saveUserAddress(userAddress);
        if (saveResult) {
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("添加失败");
    }

    @PutMapping("/address")
    @Operation(summary = "修改地址")
    public Result<Boolean> updateMallUserAddress(
            @RequestBody UpdateMallUserAddressParam updateMallUserAddressParam,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        MallUserAddress mallUserAddressById = mallUserAddressService.getMallUserAddressById(updateMallUserAddressParam.getAddressId());
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        MallUserAddress userAddress = new MallUserAddress();
        BeanUtil.copyProperties(updateMallUserAddressParam, userAddress);
        userAddress.setUserId(loginMallUser.getUserId());
        Boolean updateResult = mallUserAddressService.updateMallUserAddress(userAddress);
        if (updateResult) {
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("修改失败");
    }

    @GetMapping("/address/{addressId}")
    @Operation(summary = "获取收货地址详情", description = "传参为地址id")
    public Result<NewBeeMallUserAddressVO> getMallUserAddress(
            @Parameter(description = "地址id") @PathVariable("addressId") Long addressId,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        MallUserAddress mallUserAddressById = mallUserAddressService.getMallUserAddressById(addressId);
        NewBeeMallUserAddressVO newBeeMallUserAddressVO = new NewBeeMallUserAddressVO();
        BeanUtil.copyProperties(mallUserAddressById, newBeeMallUserAddressVO);
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        return ResultGenerator.genSuccessResult(newBeeMallUserAddressVO);
    }

    @GetMapping("/address/default")
    @Operation(summary = "获取默认收货地址", description = "无传参")
    public Result getDefaultMallUserAddress(
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        MallUserAddress mallUserAddressById = mallUserAddressService.getMyDefaultAddressByUserId(loginMallUser.getUserId());
        return ResultGenerator.genSuccessResult(mallUserAddressById);
    }

    @DeleteMapping("/address/{addressId}")
    @Operation(summary = "删除收货地址", description = "传参为地址id")
    public Result deleteAddress(
            @Parameter(description = "地址id") @PathVariable("addressId") Long addressId,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser) {
        MallUserAddress mallUserAddressById = mallUserAddressService.getMallUserAddressById(addressId);
        if (!loginMallUser.getUserId().equals(mallUserAddressById.getUserId())) {
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        Boolean deleteResult = mallUserAddressService.deleteById(addressId);
        if (deleteResult) {
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }
}